package com.lgx.mall.web.controller;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.IntraCity;
import com.lgx.common.service.IIntraCityService;
import com.lgx.common.web.Constants;
import com.lgx.mall.business.entity.Order;
import com.lgx.mall.business.entity.OrderDetail;
import com.lgx.mall.business.service.IOrderDetailService;
import com.lgx.mall.business.service.IOrderService;
import com.lgx.right.entity.User;


@Controller
@RequestMapping("/basic")
public class BasicController {
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IOrderDetailService orderDetailService;
	
	@Autowired
	private IIntraCityService intraCityService;

	public IIntraCityService getIntraCityService() {
		return intraCityService;
	}

	public void setIntraCityService(IIntraCityService intraCityService) {
		this.intraCityService = intraCityService;
	}

	public IOrderDetailService getOrderDetailService() {
		return orderDetailService;
	}

	public void setOrderDetailService(IOrderDetailService orderDetailService) {
		this.orderDetailService = orderDetailService;
	}

	public IOrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}

	@RequestMapping("/miniNav.ftl")
	public String miniNav(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		if(user != null)
			request.setAttribute("user", user);
		
		return "basic/miniNav";
	}
	
	@RequestMapping("/header.ftl")
	public String header(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		//获取同城
		IntraCity city = (IntraCity) request.getSession().getAttribute(Constants.INTRA_CITY_SIGN);
		if(city == null){
			//获取访问者IP
			String ip = request.getRemoteAddr();
			
			URL url = Constants.class.getResource("");
			File file = null;
			try {
				file = new File(url.toURI());
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			
			if(file != null){
				//根据IP获取当前访问所在城市
				city = this.intraCityService.findIntraCityByIP(ip,Constants.INTRA_CITY_DATA_NAME, file.getAbsolutePath());
			}
			
			//当城市不存在时，默认“长沙市”
			if(city == null)city = this.intraCityService.findIntraCityById("100");
			request.getSession().setAttribute(Constants.INTRA_CITY_SIGN, city);
		}
		
		request.setAttribute("city", city);
		
		List<IntraCity> citys = this.intraCityService.findIntraCityList();
		
		request.setAttribute("citys", citys);
		
		return "basic/header";
	}
	
	@RequestMapping("/changeCity.htm")
	@ResponseBody
	public String changeCity(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		String id = request.getParameter("id");
		
		IntraCity city = this.intraCityService.findIntraCityById(id);
		
		//获取同城
		if(city == null){
			//获取访问者IP
			String ip = request.getRemoteAddr();
			
			URL url = Constants.class.getResource("");
			File file = null;
			try {
				file = new File(url.toURI());
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			
			if(file != null){
				//根据IP获取当前访问所在城市
				city = this.intraCityService.findIntraCityByIP(ip,Constants.INTRA_CITY_DATA_NAME, file.getAbsolutePath());
			}
			
			//当城市不存在时，默认“长沙市”
			if(city == null)city = this.intraCityService.findIntraCityById("100");
		}
		
		request.getSession().setAttribute(Constants.INTRA_CITY_SIGN, city);
		
		request.setAttribute("city", city);
		
		List<IntraCity> citys = this.intraCityService.findIntraCityList();
		request.setAttribute("citys", citys);
		return "location.reload();";
	}
	
	@RequestMapping("/nav.ftl")
	public String nav(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		String aid = request.getParameter("aid");
		
		if(!StringUtils.isEmpty(aid))request.setAttribute("aid", aid);
		
		request.setAttribute("basePath", basePath);
		
		//初始化导航栏
		request.setAttribute("dicts", Constants.dicts);
		
		//初始化导航栏分类
		request.setAttribute("categorys", Constants.categorys);
		
		//加载购物车,当用户已登录时
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		if(user != null){
			//获取购物车
			Order order = this.orderService.findOrderByUserId(user.getId());
			
			//购物车商品详细
			List<OrderDetail> details = this.orderDetailService.findOrderDetailList(order.getId());
			
			String detailIds = "";
			
			for(OrderDetail detail : details){
				if(!StringUtils.isEmpty(detailIds))detailIds+=",";
				detailIds += detail.getId();
			}
			
			request.setAttribute("order", order);
			request.setAttribute("details", details);
			request.setAttribute("detailIds", detailIds);
		}
		
		return "basic/nav";
	}
	
	@RequestMapping("/guide.ftl")
	public String guide(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		return "basic/guide";
	}
	
	@RequestMapping("/partner.ftl")
	public String partner(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		return "basic/partner";
	}
	
	@RequestMapping("/foot.ftl")
	public String foot(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		return "basic/foot";
	}
}
